module view {
    //建造	
    export class BuildView extends eui.Component {
        closeBtn: eui.Button
        buildGroup: eui.Group
        public constructor() {
            super()
            this.addEventListener(eui.UIEvent.COMPLETE,this.onComplete,this)
            this.skinName = "resource/skin/BuildSkin.exml"
        }

        protected onComplete(): void {
            this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
            for(var i in totalConfig[410000].builds) {
                var buildBlock = new BuildBlock(i, this)
                this.buildGroup.addChild(buildBlock)
            }
        }

        protected close(): void {
            this.parent.removeChild(this)
        }
    }

    class BuildBlock extends eui.Component {
        progressBar: eui.ProgressBar
        buildbtn: eui.Button
        btn: eui.Button
        completeTime: number
        txt: string
        buildPart: string
        timer: egret.Timer
        explainBtn: eui.Button
        buildView: BuildView
        explainView: ExplainView
        public constructor(buildPart, buildView:BuildView) {
            super()
            this.skinName = "resource/skin/BuildBlockSkin.exml"
            this.buildPart = buildPart
            this.buildView = buildView
            this.btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.build,this)
            this.buildbtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.explain,this)
            this.explainBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.explain,this)
            this.buildbtn.addEventListener(egret.TouchEvent.TOUCH_END,this.explainClose,this)
            this.explainBtn.addEventListener(egret.TouchEvent.TOUCH_END,this.explainClose,this)
            this.buildbtn.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE,this.explainClose,this)
            this.explainBtn.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE,this.explainClose,this)
            this.showBuildInfo()
        }
        
        private explain(evt:egret.TouchEvent){
            var buildConfig = totalConfig[410000].builds[this.buildPart][0]
            this.explainView = new ExplainView(buildConfig)
            this.buildView.addChild(this.explainView)
            this.explainView.show()
        }
        
        private explainClose(evt: egret.TouchEvent) { 
            this.buildView.removeChild(this.explainView)
        }
        
        private showBuildInfo(){
            if(gplayer.buildings[this.buildPart].lvl > 0){
                this.progressBar.visible = false
                this.btn.visible = false
                return
            }
            var buildConfig = totalConfig[410000].builds[this.buildPart][0]
            this.txt = ''
            for(var i in buildConfig.items) {
                var item = buildConfig.items[i]
                var itemName = templateName[item[0]]
                var num = item[1]
                this.txt += itemName + 'X' + num
            }
            this.progressBar.labelFunction = this.barLabelFunction
            this.progressBar['txt'] = this.txt         
            var nowTime = new Date().getTime()
            var completeTime = gplayer.buildings[this.buildPart].completeTime
            if(completeTime !== 0 && completeTime > nowTime) { 
                this.btn.visible = false
                var per = 1 - (completeTime - nowTime) / buildConfig.cd
                this.progressBar.maximum = buildConfig.cd / 250
                this.progressBar.slideDuration =  250
                this.progressBar.value = Math.floor(per * this.progressBar.maximum)     
                this.timer = new egret.Timer(250,(completeTime - nowTime) / 250 + 1)
                this.timer.addEventListener(egret.TimerEvent.TIMER,this.timerHandler,this)
                this.timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE,this.timerComFunc,this)
                this.timer.start()
            }else{
                this.progressBar.value = 0 
            }
        }

        private barLabelFunction(value: number,maximum: number): string {           
            if(value === 0) {
                return this.txt
            }
        }
        
        private build(){
            var buildConfig = totalConfig[410000].builds[this.buildPart][0]
            var ret = game.utilsHandle.removeItemsAndGold(gplayer, buildConfig)
            if(ret === -1) {
                return
            }
            var nowTime = new Date().getTime()
            gplayer.buildings[this.buildPart].completeTime = buildConfig.cd + nowTime   
            this.showBuildInfo()
        }
       
        
        private timerHandler(evt: egret.TimerEvent){
            this.progressBar.value += 1
        }
        
        private timerComFunc(evt: egret.TimerEvent){
            this.progressBar.visible = false
            var nowTime = new Date().getTime()
            var completeTime = gplayer.buildings[this.buildPart].completeTime
            if(completeTime !== 0 && completeTime < nowTime) { 
                gplayer.buildings[this.buildPart].completeTime = 0
                gplayer.buildings[this.buildPart].lvl += 1
            }
        }
    }
    
    class ExplainView extends eui.Component{
        nameLabel : eui.Label
        contentLabel : eui.Label
        info :any
        public constructor(info){
            super()
            this.skinName = "resource/skin/ExplainSkin.exml"
            this.info = info
        }
        
        public show(){
            this.horizontalCenter = 0
            this.y = this.stage.height / 2
            this.nameLabel.text = this.info.name
            this.contentLabel.text = '介绍说明'
        }
    }
}
